package com.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import java.io.IOException;

@Configuration
public class RedissonConfig {

    @Bean(name="redissonProperties")
    @ConfigurationProperties(prefix = "redisson")
    public RedissonProperties redissonProperties(){
        return new RedissonProperties();
    }


    @Bean(name="redissonClient")
    public RedissonClient redissonClient() throws IOException {
        RedissonClient redissonClient = null;
            Config config=Config.fromYAML(this.redissonProperties().getConfigLocation().getInputStream());
            redissonClient = Redisson.create(config);
        return redissonClient;
    }

    public class RedissonProperties{
        private Resource configLocation;
        public Resource getConfigLocation() {
            return configLocation;
        }
        public void setConfigLocation(Resource configLocation){
            this.configLocation = configLocation;
        }
    }
}
